package DirectoryViewer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: Андрей
 * Date: 09.12.13
 * Time: 8:08
 * To change this template use File | Settings | File Templates.
 */

public class Controller
{
    public static String path;
    public static void main(String[] args) throws Exception
    {
        System.out.println();
        System.out.println("Interface of this directory viever: command,[command[,command...]] directory");
        System.out.println("if command is 'find' then 'find obj', where 'obj'-name of searching file" );
        System.out.println("For closing the program, enter 'exit'");
        String cmd;
        do
        {
        System.out.println("command:");
        Scanner commandScanner=new Scanner(System.in);
        cmd=    commandScanner.nextLine();
        ArrayList<String> commands=CommandParser(cmd);

        for(int i=0;i<commands.size();i++)
        {
            File f=new File(path);
            if(f.isDirectory())
            {
                if(commands.get(i).equals("dir"))
                 {
                      dir sd=new dir();        //static is not requed in interface's  (want to do dir.execute)
                      sd.execute(path.toString());
                 }
                if(commands.get(i).equals("find"))
                {
                    find fid=new find();
                    i++;
                   fid.execute(path + " " + commands.get(i));
                }
                if(commands.get(i).equals("tree"))
                {
                    tree tr=new tree();
                    i++;
                    tr.execute(path);
                }
            }
            else
            {
                System.out.println("директория задана неверно!");
            }
        }
        }while (cmd.equals("exit"));

    }

    private static ArrayList<String> CommandParser(String str)   //parser for command
    {

        ArrayList<String> cmd=new ArrayList<String>();
        StringBuffer strB=new StringBuffer();
        try
        {
        for (int i=0;i<str.length();i++)
        {
            if(str.charAt(i)==' ')
            {
                if(Controller.isCommand(strB.toString()))
                {
                    cmd.add(strB.toString());
                    if(strB.toString().equals("find"))
                    {
                        int j=i;
                        i++;
                        while (str.charAt(i)!=' ')
                        {
                            i++;
                        }
                        cmd.add(str.substring(j+1,i));

                    }
                    strB.delete(0,strB.length());

                    continue;
                }
                strB.delete(0, strB.length());
                continue;
            }
            strB.append(str.charAt(i));
        }
        int i=str.length()-1;
        while(str.charAt(i)!=' ')
        {
            i--;
        }
        path=str.substring(i+1,str.length());
        }
        catch(Exception ex)
        {
            System.out.println("комманда введена неверно");
        }
        return cmd;
    }

    private static Boolean isCommand(String str)
    {
        //this is bad metod, but i cant find how to do enum(string) in java
        if((str.equals("tree"))||(str.equals("dir")) ||(str.equals("find")))
        {
            return true;
        }
        return false;

    }
}
